package com.woniuxy.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.woniuxy.dao.WalletDao;
import com.woniuxy.entity.Wallet;
import com.woniuxy.service.WalletService;

@Service
public class WallletServiceImpl implements WalletService{
	@Autowired
	WalletDao walletDao;

	/**
	 * @author 曾令忠
	 * 修改指定账户的金额
	 * id:wallet id；num:金额
	 */
	@Override
	@Transactional
	public void deposit(int id, double num) {
		Wallet w = walletDao.getMyWallet(id);
		num = w.getBalance() + num;
		walletDao.deposit(id, num);
	}

	/**
	 * @author 曾令忠
	 * 添加账户
	 * id:wallet id
	 */
	@Override
	@Transactional
	public void addWallet(long id) {
		walletDao.addWallet(id);
		
	}

	/**
	 * @author 曾令忠
	 * 提现
	 * id:用户id；amount:金额
	 */
	@Override
	public int outPay(long id, String amount) {
		int uid = (int)id;
		Wallet w = walletDao.getMyWalletByUserId(uid);
		double num = w.getBalance()-Double.parseDouble(amount);
		System.out.println(w.getUserId()+"的余额："+w.getBalance());
		System.out.println(num);
		if(num<0) {
			return 2;
		}
		walletDao.deposit(uid, num);
		return 1;
		
	}
}
